package com.eoner.managerlibrary.user.dispatch;

import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.commonbean.user.CustomerBean;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.managerlibrary.user.callback.UserDataCallBack;
import com.eoner.managerlibrary.user.request.UserDataRetrofit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserDataRequest {

    private boolean isRequestCustomerInfo = false;

    /**
     * 获取用户信息
     * @param callBack
     */
    public void requestCustomerInfo(final UserDataCallBack callBack){
        if (!UserDataManager.getInstance().isSystemLogin()) {
            return;
        }
        if (UserDataManager.getInstance().getMobile() !=null && UserDataManager.getInstance().getMobile().length() > 0) {
            //TODO:存在邀请人手机号就不请求
            return;
        }
        isRequestCustomerInfo = true;
        Observable<CustomerBean> observable = UserDataRetrofit.getInstance().getCustomerInfo();
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CustomerBean>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }
            @Override
            public void onNext(@io.reactivex.annotations.NonNull CustomerBean bean) {
                isRequestCustomerInfo = false;
                if("0".equals(bean.getCode())){
                    callBack.progressCustomerData(bean);
                    callBack.onSuccess();
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                isRequestCustomerInfo = false;
                callBack.onFailed(e.toString());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 获取用户信息
     * @param callBack
     */
    public void requestCustomerInfoWithoutMobile(String customToken,final UserDataCallBack callBack){
        if (isRequestCustomerInfo) {
            return;
        }
        isRequestCustomerInfo = true;
        UserDataRetrofit.getInstance().getCustomerInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CustomerBean>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }
            @Override
            public void onNext(@io.reactivex.annotations.NonNull CustomerBean bean) {
                isRequestCustomerInfo = false;
                if("0".equals(bean.getCode())){
                    callBack.progressCustomerData(bean);
                    callBack.onSuccess();
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                isRequestCustomerInfo = false;
                callBack.onFailed(e.toString());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 获取用户信息 - 强制刷新
     * @param callBack
     */
    public void requestCustomerInfoForceUpdate(final UserDataCallBack callBack) {
        if (!UserDataManager.getInstance().isSystemLogin()) {
            return;
        }
        UserDataRetrofit.getInstance().getCustomerInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CustomerBean>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }
            @Override
            public void onNext(@io.reactivex.annotations.NonNull CustomerBean bean) {
                if("0".equals(bean.getCode())){
                    callBack.progressCustomerData(bean);
                    callBack.onSuccess();
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                callBack.onFailed(e.toString());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }



    /**
     * 获取用户订单信息
     * */
    public void requestCustomOrderIndex(final UserDataCallBack callBack) {
        UserDataRetrofit.getInstance().customerOrderInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CustomerOrderInfo>() {
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }
            @Override
            public void onNext(@io.reactivex.annotations.NonNull CustomerOrderInfo bean) {
                if (bean!=null && bean.getData()!=null){
                    callBack.progressCustomOrderData(bean);
                    callBack.onSuccess();
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                callBack.onFailed(e.toString());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });

    }

}
